package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.entity.StudentFaceData;
import com.xmy.cultivate.entity.StudentFaceManyDetail;
import com.xmy.cultivate.mapper.StudentFaceDataMapper;
import com.xmy.cultivate.mapper.StudentFaceManyDetailMapper;
import com.xmy.cultivate.service.IMqPushService;
import com.xmy.cultivate.service.IOrganizationService;
import com.xmy.cultivate.service.IStudentFaceManyDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 人脸采集 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-04-19
 */
@Service
public class StudentFaceManyDetailServiceImpl extends ServiceImpl<StudentFaceManyDetailMapper, StudentFaceManyDetail> implements IStudentFaceManyDetailService {

    @Autowired
    @Lazy
    StudentFaceDataMapper studentFaceDataMapper;

    @Autowired
    @Lazy
    IOrganizationService iOrganizationService;

    @Autowired
    @Lazy
    IMqPushService iMqPushService;

    @Autowired
    IdWorker idWorker;

    //添加数据到多个人脸库
    @Override
    public boolean addDataBatch(StudentFaceData studentFaceData, Long schoolId, Integer faceType) {
        try{
            Organization organizationOld = iOrganizationService.getById(schoolId);

            QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
            organizationQueryWrapper.eq("type",1);
            organizationQueryWrapper.eq("status",1);
            organizationQueryWrapper.groupBy("ali_account");
            List<Organization> organizationList = iOrganizationService.list(organizationQueryWrapper);
            for (Organization organization:organizationList){
                StudentFaceManyDetail studentFaceManyDetail = new StudentFaceManyDetail();
                BeanUtils.copyProperties(studentFaceData,studentFaceManyDetail);
                studentFaceManyDetail.setId(idWorker.nextId());
                studentFaceManyDetail.setAliAccount(organization.getAliAccount());
                studentFaceManyDetail.setMqQueueKey(organization.getMqQueueKey());
                studentFaceManyDetail.setFaceType(faceType);
                if (organization.getAliAccount().equals(organizationOld.getAliAccount())){
                    studentFaceManyDetail.setPushMqType(2);//主图，表示已经添加到过人脸库
                    studentFaceManyDetail.setType(1);
                }else {
                    studentFaceManyDetail.setEntityId(null);
                    studentFaceManyDetail.setQualitieScore(null);
                }
                studentFaceManyDetail.setMqQueueKey(organization.getMqQueueKey());
                this.save(studentFaceManyDetail);
                iMqPushService.addStudentFaceManyDetail(studentFaceManyDetail);
            }
        }catch (Exception e){
        }

        return true;
    }

    /**
     * 更换人脸库初始化使用
     * @param studentFaceData
     * @return
     */
    @Override
    public boolean addDataBatchInit(StudentFaceData studentFaceData) {
        try{
            QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
            organizationQueryWrapper.eq("type",1);
            organizationQueryWrapper.eq("status",1);
            organizationQueryWrapper.groupBy("ali_account");
            List<Organization> organizationList = iOrganizationService.list(organizationQueryWrapper);
            for (Organization organization:organizationList){
                StudentFaceManyDetail studentFaceManyDetail = new StudentFaceManyDetail();
                BeanUtils.copyProperties(studentFaceData,studentFaceManyDetail);
                QueryWrapper<StudentFaceManyDetail> studentFaceManyDetailQueryWrapper = new QueryWrapper<>();
                studentFaceManyDetailQueryWrapper.eq("ali_account",organization.getAliAccount());
                studentFaceManyDetailQueryWrapper.eq("oss_image",studentFaceData.getOssImage());
                Long count = this.count(studentFaceManyDetailQueryWrapper);
                if (count > 0){
                    continue;
                }
                studentFaceManyDetail.setId(idWorker.nextId());
                studentFaceManyDetail.setAliAccount(organization.getAliAccount());
                studentFaceManyDetail.setMqQueueKey(organization.getMqQueueKey());
                studentFaceManyDetail.setEntityId(null);
                studentFaceManyDetail.setQualitieScore(null);
                studentFaceManyDetail.setMqQueueKey(organization.getMqQueueKey());
                this.save(studentFaceManyDetail);
                iMqPushService.addStudentFaceManyDetail(studentFaceManyDetail);
            }
        }catch (Exception e){
        }

        return true;
    }
}
